import 'package:cloud_medicine/components/shop_picture.dart';
import 'package:flutter/material.dart';

class MyRow extends StatelessWidget {
  final Widget leftwidget;
  final Widget rightwidget;
  const MyRow({Key key, @required this.leftwidget, @required this.rightwidget})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          child: leftwidget,
        ),
        Container(
          child: rightwidget,
        )
      ],
    );
  }
}

class MedicineItemList extends StatelessWidget {
  final List<Map<String, dynamic>> cartList;
  final num price;
  final num deliveryFee;

  const MedicineItemList(
      {Key key,
      @required this.deliveryFee,
      @required this.price,
      @required this.cartList})
      : super(key: key);

  List<Widget> medicineListLayout() {
    return this.cartList.map((e) => medicineItem(e)).toList();
  }

  Widget medicineItem(Map<String, dynamic> e) {
    return MyRow(
        leftwidget: Row(
          children: [
            ShopImg(
              imgUrl: e['picture'],
              imageStyle: 60,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  margin: EdgeInsets.only(
                    left: 5,
                    bottom: 5,
                  ),
                  child: Text('${e['name']}'),
                ),
                Container(
                  margin: EdgeInsets.only(left: 5),
                  child: Text('${e['number']}'),
                )
              ],
            )
          ],
        ),
        rightwidget: Text('￥${e['price'] * e['number']}'));
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Column(children: medicineListLayout()),
        Divider(
          thickness: 1,
          color: Colors.grey[300],
        ),
        MyRow(
            leftwidget: Text('配送费'), rightwidget: Text('￥${this.deliveryFee}')),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              margin: EdgeInsets.only(right: 10),
              child: Text('合计'),
            ),
            Container(
              child: Text('￥${this.price}'),
            )
          ],
        ),
      ],
    );
  }
}
